package com.ziyun.pop.mall.module.order.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.service.common.constant.AppType;
import com.service.common.constant.OrderStatus;
import com.service.common.model.PageResult;
import com.service.order.api.OrderService;
import com.service.order.constant.OrderResult;
import com.service.order.model.order.GoodsCommentParamDto;
import com.service.order.model.order.OrderCancelParamDto;
import com.service.order.model.order.OrderFilesParamDto;
import com.service.order.model.order.OrderInfoRequestVo;
import com.service.order.model.order.QueryOrderParamDto;
import com.service.utils.push.umeng.JsonResult;
import com.utility.JsonUtils;
import com.ziyun.pop.mall.system.config.HttpRequestLocal;

/**
 * OrderInfo 接口
 */
@Controller
public class MallOrderInfoController {

	private final Log log = LogFactory.getLog(this.getClass());
	private static final String MODEL = "/order/orderInfo";
	@Autowired
	private OrderService orderService;

	@Autowired
	private HttpRequestLocal httpRequestLocal;

	/**
	 * 进入订单列表页面
	 *
	 * @param orderStatus
	 * @param orderId
	 * @param page
	 * @return
	 * @throws IOException
	 */
	@GetMapping(MODEL + "/lookOrders.do")
	public ModelAndView lookOrders(QueryOrderParamDto orderQueryParamDto) throws IOException {
		ModelAndView view = new ModelAndView("/order/orderList/index.html");
		if (orderQueryParamDto.getOrderStatus() == null) {
			orderQueryParamDto.setOrderStatus(OrderStatus.valueOf("all"));
		}
		view.addObject("orderQueryParamDto", orderQueryParamDto);
		return view;
	}

	/**
	 * 获取订单列表
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(MODEL + "/getOrderInfoRequestVos.json")
	@ResponseBody
	public JsonResult<PageResult<OrderInfoRequestVo>> getOrderInfoRequestVos(QueryOrderParamDto orderQueryParamDto) {
		if (orderQueryParamDto.getOrderStatus() == null) {
			orderQueryParamDto.setOrderStatus(OrderStatus.valueOf("all"));
		}
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		OrderResult<PageResult<OrderInfoRequestVo>> orderResult = orderService.listOrderInfo(AppType.pc, userId,
				orderQueryParamDto);
		PageResult<OrderInfoRequestVo> orderInfoRequestVoPage = (PageResult<OrderInfoRequestVo>) orderResult.getData();
		return JsonResult.success(orderInfoRequestVoPage);
	}

	/**
	 * 进入订单详情页
	 *
	 * @param orderId
	 * @return
	 * @throws IOException
	 */
	@GetMapping(MODEL + "/lookOrderDetail.do")
	public ModelAndView lookOrderDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String orderId = request.getParameter("orderId");
		ModelAndView view = new ModelAndView("/order/orderList/indexDetail.html");
		view.addObject("orderId", orderId);
		return view;
	}

	/**
	 * 根据订单号获取订单详情
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(MODEL + "/getOrderInfoRequestVoById.json")
	@ResponseBody
	public JsonResult<OrderInfoRequestVo> getOrderInfoRequestVoById(HttpServletRequest request,
			HttpServletResponse response) {
		String orderId = request.getParameter("orderId");
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		OrderInfoRequestVo orderResult = orderService.getOrderDetailed(userId, orderId).getData();
		return JsonResult.success(orderResult);
	}

	/**
	 * 取消订单
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@PostMapping(MODEL + "/updateOrderCancel.json")
	@ResponseBody
	public JsonResult<?> updateOrderCancel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String requestData = request.getParameter("requestData");
		OrderCancelParamDto orderCancelParamDto = JsonUtils.string2Object(requestData, OrderCancelParamDto.class);
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		orderService.updateOrderCancel(userId, orderCancelParamDto);
		return JsonResult.success();
	}

	/**
	 * 确认收货
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(MODEL + "/updateOrderConfirmShip.json")
	@ResponseBody
	public JsonResult<?> updateOrderConfirmShip(HttpServletRequest request, HttpServletResponse response) {
		String orderId = request.getParameter("orderId");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderId", orderId);
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		orderService.updateOrderConfirmShip(userId, map);
		return JsonResult.success();
	}

	/**
	 * 删除
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(MODEL + "/updateOrderDel.json")
	@ResponseBody
	public JsonResult<?> updateOrderDel(HttpServletRequest request, HttpServletResponse response) {
		String orderId = request.getParameter("orderId");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderId", orderId);
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		orderService.updateOrderDel(userId, map);
		return JsonResult.success();
	}

	/**
	 * 上传附件
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(MODEL + "/updateOrderFiles.json")
	@ResponseBody
	public JsonResult<Map<Integer, Integer>> updateOrderFiles(HttpServletRequest request,
			HttpServletResponse response) {
		String requestData = request.getParameter("requestData");
		OrderFilesParamDto orderFilesParamDto = JsonUtils.string2Object(requestData, OrderFilesParamDto.class);
		Map<Integer, Integer> fileMap = new HashMap<Integer, Integer>();
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		fileMap = orderService.updateOrderFiles(userId, orderFilesParamDto).getData();
		return JsonResult.success(fileMap);
	}

	/**
	 * 删除附件
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(MODEL + "/updateOrderFilesDel.json")
	@ResponseBody
	public JsonResult<?> updateOrderFilesDel(HttpServletRequest request, HttpServletResponse response) {
		String foIds = request.getParameter("foIds");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("foIds", foIds);
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		orderService.updateOrderFilesDel(userId, map);
		return JsonResult.success();
	}

	/**
	 * 评论
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(MODEL + "/saveOrderGoodsComment.json")
	@ResponseBody
	public JsonResult<?> saveOrderGoodsComment(HttpServletRequest request, HttpServletResponse response) {
		String requestData = request.getParameter("requestData");
		GoodsCommentParamDto goodsCommentParamDto = JsonUtils.string2Object(requestData, GoodsCommentParamDto.class);
		// Map<Integer, Integer> fileMap = new HashMap<Integer, Integer>();
		orderService.saveOrderGoodsComment(goodsCommentParamDto);
		return JsonResult.success();
	}

}
